<template>
  <view class="edit-container">
    <view class="form-item">
      <text class="label">所在城市</text>
      <input class="input" v-model="formData.city" placeholder="请输入城市" />
    </view>
    
    <view class="form-item">
      <text class="label">小区/大厦/学校</text>
      <input class="input" v-model="formData.department" placeholder="请输入详细地址" />
    </view>
    
    <view class="form-item">
      <text class="label">楼号-门牌号</text>
      <input class="input" v-model="formData.houseNumber" placeholder="例如: 2号楼10号" />
    </view>
    
    <view class="form-item">
      <text class="label">收货人</text>
      <input class="input" v-model="formData.name" placeholder="请输入收货人姓名" />
    </view>
    
    <view class="form-item">
      <text class="label">联系电话</text>
      <input class="input" v-model="formData.phone" placeholder="请输入联系电话" type="number" />
    </view>
    
    <button class="save-btn" @click="saveAddress">保存</button>
  </view>
</template>

<script>
import config from '../config.js';
export default {
  data() {
    return {
      formData: {
        city: '',
        department: '',
        houseNumber: '',
        name: '',
        phone: ''
      }
    }
  },
  onLoad(options) {
    if (options.id) {
      this.loadAddressDetail(options.id)
    }
  },
	 
  methods: {
    async loadAddressDetail(id) {
      try {
        const res = await uni.request({
          url: config.api_host+`/address/${id}`
        })
        this.formData = res[1].data
      } catch (error) {
        uni.showToast({
          title: '地址加载失败',
          icon: 'none'
        })
      }
    },
    async saveAddress() {
      if (!this.validateForm()) return
      
      try {
        if (this.formData.id) {
          // 更新地址
          await uni.request({
            url: config.api_host+`/address/${this.formData.id}`,
            method: 'PUT',
            data: this.formData
          })
        } else {
          // 新增地址
          await uni.request({
            url: config.api_host+'/address',
            method: 'POST',
            data: this.formData
          })
        }
        
        uni.showToast({
          title: '保存成功',
          icon: 'success'
        })
        
        setTimeout(() => {
          uni.navigateBack()
        }, 1500)
      } catch (error) {
        uni.showToast({
          title: '保存失败',
          icon: 'none'
        })
      }
    },
    validateForm() {
      if (!this.formData.city) {
        uni.showToast({ title: '请输入城市', icon: 'none' })
        return false
      }
      if (!this.formData.department) {
        uni.showToast({ title: '请输入详细地址', icon: 'none' })
        return false
      }
      if (!this.formData.name) {
        uni.showToast({ title: '请输入收货人姓名', icon: 'none' })
        return false
      }
      if (!this.formData.phone || !/^1[3-9]\d{9}$/.test(this.formData.phone)) {
        uni.showToast({ title: '请输入正确的手机号', icon: 'none' })
        return false
      }
      return true
    }
  }
}
</script>

<style>
.edit-container {
  padding: 30rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.input {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
}

.save-btn {
  margin-top: 60rpx;
  background-color: #4e71a7;
  color: white;
}
</style>

